var errorUserid = true;
var errorPassword = true;
var errorConfirmPwd = true;
var errorEmail = true;
var errorCorporationName = true;
var errorOfficePhone = true;
var errorAgreement = true;
var WSContextPath;

function checkUserId(userid) {
	var pattern = /[a-zA-Z0-9]{4,20}/;
	
	$("#error_message").html("");
	$("#user_id").css("background", "#ffffff");
	errorUserid = false;
	if (userid == "" || !pattern.test(userid)) {
		$("#error_message").html("用户名错误<br/>");
		$("#user_id").css("background", "#fbe2e2");
		errorUserid = true;
	} else {
		requestUrl = "check_user_id";
		$.post(requestUrl, {user_id:userid}, function handlerCheckUserId(ret) {
			if (ret != "true") {
				$("#error_message").html("用户名已存在<br/>");
				$("#user_id").css("background", "#fbe2e2");
				errorUserid = true;
			} else {
				errorUserid = false;
			}
		});
	}
	return true;
}

function checkPassword(password) {
	var pattern = /[a-zA-Z0-9]{4,20}/;
	
	if (password == "" || !pattern.test(password)) {
		$("#error_message").html("密码错误<br/>");
		$("#password").css("background", "#fbe2e2");
		errorPassword = true;
	} else {
		$("#error_message").html("");
		$("#password").css("background", "#ffffff");
		errorPassword = false;
	}
	return true;
}

function checkConfirmPwd(confirmPwd) {
	if ($("#password").val() != confirmPwd) {
		$("#error_message").html("确认密码错误<br/>");
		$("#confirm_pwd").css("background", "#fbe2e2");
		errorConfirmPwd = true;
	} else {
		$("#error_message").html("");
		$("#confirm_pwd").css("background", "#ffffff");
		errorConfirmPwd = false;
	}
	return true;
}

function checkEmail(email) {
	var pattern = /^[^\s@]+@[^\s@]+$/;
	
	if (email == "" || !pattern.test(email)) {
		$("#error_message").html("邮箱错误<br/>");
		$("#email").css("background", "#fbe2e2");
		errorEmail = true;
	} else {
		$("#error_message").html("");
		$("#email").css("background", "#ffffff");
		errorEmail = false;
	}
	return true;
}

function checkCorporationName(corporationName) {
	var parter = /^\S+$/;
	
	if (corporationName == "" || !parter.test(corporationName)) {
		$("#error_message").html("公司名称错误<br/>");
		$("#corporation_name").css("background", "#fbe2e2");
		errorCorporationName = true;
	} else {
		$("#error_message").html("");
		$("#corporation_name").css("background", "#ffffff");
		errorCorporationName = false;
	}
	return true;
}

function checkOfficePhone(phone) {
	var pattern = /^[0-9]{8}/;
	
	if (phone == "" || !pattern.test(phone)) {
		$("#error_message").html("固定电话错误<br/>");
		$("#office_phone").css("background", "#fbe2e2");
		errorOfficePhone = true;
	} else {
		$("#error_message").html("");
		$("#office_phone").css("background", "#ffffff");
		errorOfficePhone = false;
	}
	return true;
}

function checkAgreement() {	
	if ($("#agreement").attr("checked") != "checked") {
		$("#error_message").html("请阅读并同意注册协议<br/>");
		errorAgreement = true;
	} else {
		$("#error_message").html("");
		errorAgreement = false;
	}
	return true;
}

function mechantRegister() {
	var userid = $("#user_id").val();
	var password = $("#password").val();
	var confirmPwd = $("#confirm_pwd").val();
	var email = $("#email").val();
	var corporationName = $("#corporation_name").val();
	var phone = $("#office_phone").val();

	if (checkUserId(userid) && !errorUserid
			&& checkPassword(password) && !errorPassword
			&& checkConfirmPwd(confirmPwd) && !errorConfirmPwd
			&& checkEmail(email) && !errorEmail
			&& checkCorporationName(corporationName) && !errorCorporationName
			&& checkOfficePhone(phone) && !errorOfficePhone
			&& checkAgreement() && !errorAgreement) {
		$("#frm_register").submit();
	}
}

function freshImage(data) {
	var result = eval('(' + data + ')');
	var imageName = "";

	if (result.code == "0") {
		imageName = result.resultData;
		document.getElementById("img_merchant").src = WSContextPath + "/image/uploads/" + imageName;
	} else {
		showMessage(result.message);
	}
	document.getElementById("image_name").value = imageName;
}